/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package editdistance;

/**
 *
 * @author julio.zanoni
 */

public class EditDistance {

    public int AlgorithmEditDistance (String str1, String str2) {
        char[] arrayStr1 = str1.toCharArray();
        char[] arrayStr2 = str2.toCharArray();
        int lenghtStr1 = str1.length();
        int lenghtStr2 = str2.length();
        int matrix[][] = new int [lenghtStr1 + 1][lenghtStr2 + 1];

        for (int i = 0; i < lenghtStr1; i++) {
            matrix[i][0] = i;
        }

        for (int j = 0; j < lenghtStr2; j++) {
            matrix[0][j] = j;
        }

        int C = 0;

        for (int i = 1; i <= lenghtStr1; i++) {
            for (int j = 1; j <= lenghtStr2; j++) {
                if (arrayStr1 [i - 1] == arrayStr2[j - 1]) {
                    C = 0;
                }
                else {
                    C = 1;
                }
               int minMatrix1 = Math.min (matrix[i - 1][j - 1] + C, matrix[i - 1][j] + 1);
                int minMatrix = Math.min (minMatrix1, matrix[i][j - 1] + 1);
                matrix[i][j] = minMatrix;
            }
        }
        return matrix[lenghtStr1][lenghtStr2];
    }
}
